//
//  LYCityViewController.m
//  artist
//
//  Created by Jay_Apple on 16/3/6.
//  Copyright © 2016年 viviyoo. All rights reserved.
//

#import "LYCityViewController.h"
#import "LYCityHeaderView.h"
#import "LYCityDataDBTool.h"
#import "LYCityDataModel.h"
#import "VVYCityViewCell.h"
#import "VVYCityViewCellModel.h"
#import "LYCitySectionHeaderView.h"

@interface LYCityViewController ()<UITableViewDataSource, UITableViewDelegate, VVYCityViewCellDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (strong, nonatomic) NSArray *cityModels;

@property (weak, nonatomic) LYCityHeaderView *header;

@end

@implementation LYCityViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.tableFooterView = [[UIView alloc] init];
    self.navigationItem.title = @"城市选择";
    self.cityModels = [LYCityDataDBTool getOpenCityData];
    [self locate];
    

}

- (void)viewDidLayoutSubviews{

    [super viewDidLayoutSubviews];
    //初始化tableview
    [self setUpTableView];

}

#pragma mark - 初始化
/**
 *  初始化tableview
 */
- (void)setUpTableView{
    
    
    //header
    
    if (self.header) {
        return;
    }
    
    LYCityHeaderView *header = [LYCityHeaderView viewForNib];
    
    header.frame = CGRectMake(0, 0, LYScreenWidth, 120);
    
    self.tableView.tableHeaderView = header;
    self.header = header;

}

/**
 *  定位
 */
- (void)locate{
    
    [[VVYLocationManager sharedManager] getLocationCityInfoSuccess:^(NSString *city, CLLocationCoordinate2D coordinate) {
        
        
        [[LYDataSourceManager sharedManager] setUpLocationInfo];
        self.header.locationCity =
        [LYDataSourceManager sharedManager].locationInfo.locationCity;
    } failure:^(NSString *error) {
        
        [MBProgressHUD showOnlyText:error];
        
    }];
    
}



#pragma mark - tableViewDataSource
/**
 *  cell
 */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    VVYCityViewCell *cityCell = [VVYCityViewCell cellWithTableView:tableView];
    
    LYCityDataModel *model = self.cityModels[indexPath.section];
    
    cityCell.model = model.cityModel;
    cityCell.delegate = self;
    return cityCell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return self.cityModels.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    LYCityDataModel *model = self.cityModels[section];
    
    if (model.isOpen) {
        return 1;
    }else{
        return 0;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    LYCityDataModel *model = self.cityModels[indexPath.section];
    return model.cityModel.viewHeight;
}




/**
 *  secitonHeader
 */
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    LYCitySectionHeaderView *view = [LYCitySectionHeaderView viewForNib];
    view.frame = CGRectMake(0, 0, LYScreenWidth, LYCitySectionHeaderHeight);
    LYCityDataModel *model = self.cityModels[section];
    view.title = model.cityName;
    __weak typeof(self) weakSelf = self;
    view.block = ^{
        
        model.open = !model.isOpen;
        [weakSelf.tableView reloadData];
        
    };
    
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return LYCitySectionHeaderHeight;
}


#pragma mark - sectionHeader

- (void)cityCell:(VVYCityViewCell *)cell didSelectedCity:(NSString *)city{
    
    NSInteger cityID = [LYCityDataDBTool getIDWithName:city];
    
    if (self.block) {
        self.block(city, cityID);
    }
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

@end
